package clients;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JButton;


import shared.Stock;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;

public class SuperUserFrame extends JFrame {
	

	private Client client;
	private JPanel contentPane;
	private JTable table;
	private JButton btnModifySelectedStock;
	private ArrayList<String> stockNames;



	/**
	 * Create the frame.
	 */
	public SuperUserFrame(Client newClient) 
	
	{
		setTitle("Super User GUI");
		stockNames = new ArrayList<String>();
		this.client = newClient;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 469);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		table = new JTable();
		table.setFillsViewportHeight(true);
		table.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		table.setToolTipText("");
		table.setSurrendersFocusOnKeystroke(true);
		table.setShowVerticalLines(true);
		table.setShowHorizontalLines(true);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"Stock Name", "Exchange Name", "Number of Shares", "Price"},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
			},
			new String[] {
				"Exchange Name", "Stock Name", "Number of Shares", "Price"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(140);
		table.getColumnModel().getColumn(1).setPreferredWidth(166);
		table.getColumnModel().getColumn(2).setPreferredWidth(133);
		table.setBounds(212, 92, 444, 236);
		
		contentPane.add(table);
		
		JButton btnShowStocks = new JButton("Show All Stocks");
		btnShowStocks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
				ArrayList<Stock> stocks = client.getAllStocks();
			    DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
			    
			    while (tableModel.getRowCount() > 0)
			    	tableModel.removeRow(tableModel.getRowCount()-1);
			    
			    stockNames.clear();
			    
			    tableModel.setColumnCount(4);
			    tableModel.addRow(new Object[]{"Stock Name", "Exchange Name", "Number of Shares", "Price"});
			    
			    for (int i = 0; i < stocks.size(); i++)
			    {
			    	stockNames.add(stocks.get(i).getName());
			    	String priceString = String.format("%.2f", stocks.get(i).getPrice());
			    	String[] content = new String[]{stocks.get(i).getName(), stocks.get(i).getExchange(), ""+stocks.get(i).getNumberOfShares(), priceString};
			    	Object[] newRow  = new Object[]{content[0], content[1], content[2], content[3]};
			    	tableModel.addRow(newRow);
			    }
			    table.setModel(tableModel);
			    
				
			}
		});
		btnShowStocks.setBounds(39, 103, 146, 30);
		contentPane.add(btnShowStocks);
		
		btnModifySelectedStock = new JButton("Modify Selected Stock");
		btnModifySelectedStock.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				int selected = table.getSelectedRow();
				if (selected <= 0)
				{
					JOptionPane.showMessageDialog(null, "Please select a stock!");
					return;
				}
				
				if (selected-1 > stockNames.size()-1)
				{
					JOptionPane.showMessageDialog(null, "Please select a stock!");
					return;
				}
				
				String stockName = stockNames.get(selected - 1);
				
				if (stockName == null || stockName.length() == 0)
				{
					JOptionPane.showMessageDialog(null, "Please select a stock!");
					return;
				}
				
				StockPriceDialog dialog = new StockPriceDialog(client.queryStock(stockName), client);
				dialog.setModal(true);
				dialog.setVisible(true);
				
				ArrayList<Stock> stocks = client.getAllStocks();
			    DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
			    
			    while (tableModel.getRowCount() > 0)
			    	tableModel.removeRow(tableModel.getRowCount()-1);
			    
			    stockNames.clear();
			    
			    tableModel.setColumnCount(4);
			    tableModel.addRow(new Object[]{"Stock Name", "Exchange Name", "Number of Shares", "Price"});
			    
			    for (int i = 0; i < stocks.size(); i++)
			    {
			    	stockNames.add(stocks.get(i).getName());
			    	String priceString = String.format("%.2f", stocks.get(i).getPrice());
			    	String[] content = new String[]{stocks.get(i).getName(), stocks.get(i).getExchange(), ""+stocks.get(i).getNumberOfShares(), priceString};
			    	Object[] newRow  = new Object[]{content[0], content[1], content[2], content[3]};
			    	tableModel.addRow(newRow);
			    }
			    table.setModel(tableModel);
			}
		});
		btnModifySelectedStock.setBounds(39, 228, 146, 30);
		contentPane.add(btnModifySelectedStock);
	}
}
